package com.pos.widgets;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.pos.R;

/**
 * @Description:公共标题栏
 * @author:Cai
 * @time:2014-6-26 下午1:06:26
 */
public class ActionBarManager {
	private LinearLayout linear_title_back;
	private ImageView image_common_title_function;
	private TextView text_common_title, text_common_right;
	private Activity activity;

	public ActionBarManager(Activity activity) {
		this.activity = activity;
		linear_title_back = (LinearLayout) activity.findViewById(R.id.linear_title_back);
		image_common_title_function = (ImageView) activity.findViewById(R.id.image_common_title_function);
		text_common_title = (TextView) activity.findViewById(R.id.text_common_title);
		text_common_right = (TextView) activity.findViewById(R.id.text_common_right);
	}

	/**
	 * 初始化actionBar（标题栏）
	 * 
	 * @param activity
	 * @param title
	 *            标题
	 * @param isLeftButtonVisible
	 *            左边回退按钮是否可见
	 * @param rightRes
	 *            int类型： 为图片id，右边按钮资源id(==0不可见)；String 类型，为文字
	 * @param listener
	 *            右边功能键事件(==null不可见)
	 */
	public static void init(final Activity activity, String title, boolean isLeftButtonVisible, Object rightRes,
                            OnClickListener listener) {
		ActionBarManager actionBarManager = new ActionBarManager(activity);
		actionBarManager.text_common_title.setText(title);
		if (isLeftButtonVisible) {
			actionBarManager.linear_title_back.setVisibility(View.VISIBLE);
			actionBarManager.linear_title_back.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					try {
						InputMethodManager imm = (InputMethodManager) activity
								.getSystemService(Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(activity.findViewById(R.id.edit_common_title).getWindowToken(), 0);
						activity.finish();
					} catch (Exception e) {
						// TODO: handle exception
					}

				}
			});
		} else {
			actionBarManager.linear_title_back.setVisibility(View.GONE);
		}

		if (rightRes != null) {

			if (rightRes instanceof Integer) {
				int res = (Integer) rightRes;
				if (res != 0) {
					actionBarManager.image_common_title_function.setImageResource(res);
					actionBarManager.image_common_title_function.setVisibility(View.VISIBLE);
				}
				if (listener != null) {
					actionBarManager.image_common_title_function.setOnClickListener(listener);
					actionBarManager.image_common_title_function.setVisibility(View.VISIBLE);
				}
			} else if (rightRes instanceof String) {
				String res = (String) rightRes;
				if (!TextUtils.isEmpty(res)) {
					actionBarManager.text_common_right.setText(res);
					actionBarManager.text_common_right.setVisibility(View.VISIBLE);
				}
				if (listener != null) {
					actionBarManager.text_common_right.setOnClickListener(listener);
					actionBarManager.text_common_right.setVisibility(View.VISIBLE);
				}
			}
		}

	}
}
